package com.sixkey.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sixkey.constant.CommonConstant;
import com.sixkey.entity.Department;
import com.sixkey.entity.User;
import com.sixkey.entity.vo.DeptUserVo;
import com.sixkey.exception.ServiceException;
import com.sixkey.mapper.DepartmentMapper;
import com.sixkey.response.ResponseEnum;
import com.sixkey.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sixkey.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2023-10-22
 */
@Service
@RequiredArgsConstructor
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    private final IUserService userService;

    /**
     * 构造数据返回
     * @return
     */
    @Override
    public List<Map<String,Object>> getDepartmentList() {
        List<Department> departments = this.list();
        List<Integer> deptId = departments.stream().map(Department::getId).collect(Collectors.toList());
        List<Map<String,Object>> list =  new ArrayList<>();
        deptId.stream().forEach(item -> {
            List<User> users = userService.list(new QueryWrapper<User>().eq("dept_id", item));
            Department department = this.getById(item);
            String departmentName = department.getName();
            Map<String,Object> map = new HashMap<>();
            map.put("id",department.getId());
            map.put("name",departmentName);
            map.put("userData",users);
            list.add(map);
        });
        return list;
    }

    /**
     * 返回部门数据给用户添加部门
     * @return
     */
    @Override
    public List<Map<String, Object>> getDeptData() {
        List<Department> departments = this.list();
        List<Map<String,Object>> list =  new ArrayList<>();
        departments.stream().forEach(item -> {
            Map<String,Object> map = new HashMap<>();
            map.put("value",item.getName());
            map.put("label",item.getName());
            list.add(map);
        });
        return list;
    }

    /**
     * 删除部门
     * @param id
     */
    @Override
    public void removeDept(Integer id) {
        //根据部门id查询是否还有用户在里面
        List<User> users = userService.list(new QueryWrapper<User>().eq("dept_id", id));
        if(!CollectionUtils.isEmpty(users)){
            throw new ServiceException(ResponseEnum.HAVE_USER);
        }
        //正常删除
        this.removeById(id);
    }

    /**
     * 小程序端获取部门名称
     * @return
     */
    @Override
    public List<String> getDepartmentName() {
        List<Department> departments = this.list();
        List<String> list = new ArrayList<>();
        departments.stream().forEach(item ->{
            list.add(item.getName());
        });
        return list;
    }

    /**
     * 获取具体部门的用户
     * @return
     */
    @Override
    public List<DeptUserVo> getDepartmentUser(String deptName) {
        //根据部门名称获取部门id
        Department department = this.getOne(new QueryWrapper<Department>().eq("name", deptName));
        //根据部门id获取用户集合
        List<User> users = userService.list(new QueryWrapper<User>().eq("dept_id", department.getId()));
        //封装数据返回
        List<DeptUserVo> deptUserVos = new ArrayList<>();
        users.stream().forEach(item -> {
            DeptUserVo deptUserVo = new DeptUserVo();
            deptUserVo.setDeptName(department.getName());
            deptUserVo.setName(item.getName());
            deptUserVo.setPhone(item.getPhone());
            deptUserVo.setAvatar(item.getAvatar());
            deptUserVo.setRole(item.getRole());
            deptUserVos.add(deptUserVo);
        });
        return deptUserVos;
    }

    @Override
    public List<Map<String, Object>> getDeptNumbers(String deptName) {
        Department department = this.getOne(new QueryWrapper<Department>().eq("name", deptName));
        List<User> users = userService.list(new QueryWrapper<User>().eq("dept_id", department.getId()));
        List<Map<String, Object>> list = new ArrayList<>();
        users.stream().forEach(user -> {
            Map<String, Object> map = new HashMap<>(users.size());
            map.put("name",user.getName());
            map.put("phone",user.getPhone());
            list.add(map);
        });
        return list;
    }
}
